package com.zj.study.utils.beancopier.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.springframework.asm.Type;



/**
 * @author 赵赳
 * @since 2021/9/22 11:49
 */
public class ReflectUtils {
  public static String getDescriptors(Class<?>... classes) {
    StringBuilder builder = new StringBuilder();
    Class[] var2 = classes;
    int var3 = classes.length;

    for(int var4 = 0; var4 < var3; ++var4) {
      Class<?> clazz = var2[var4];
      builder.append(Type.getDescriptor(clazz));
    }

    return builder.toString();
  }

  public static String getClassSignature(ClassInfo superClass, ClassInfo... interfaces) {
    assert superClass != null : "SuperClass can not be null";

    if (interfaces != null && interfaces.length != 0) {
      StringBuilder builder = new StringBuilder(superClass.getDescriptors());
      ClassInfo[] var3 = interfaces;
      int var4 = interfaces.length;

      for(int var5 = 0; var5 < var4; ++var5) {
        ClassInfo i = var3[var5];
        builder.append(i.getDescriptors());
      }

      return builder.toString();
    } else {
      return superClass.getDescriptors();
    }
  }

  public static String getMethodDescriptor(Class<?> rType, Class<?>... pTypes) {
    StringBuilder builder = new StringBuilder(pTypes.length + 4);
    builder.append("(");
    Class[] var3 = pTypes;
    int var4 = pTypes.length;

    for(int var5 = 0; var5 < var4; ++var5) {
      Class<?> type = var3[var5];
      builder.append(Type.getDescriptor(type));
    }

    return builder.append(")").append(Type.getDescriptor(rType)).toString();
  }

  public static Map<String, Tuple<Field, Method>> getFieldGetters(Class<?> target) {
    Field[] fields = target.getDeclaredFields();
    Map<String, Tuple<Field, Method>> getters = new HashMap(fields.length, 1.0F);
    Field[] var3 = fields;
    int var4 = fields.length;

    for(int var5 = 0; var5 < var4; ++var5) {
      Field field = var3[var5];

      Method getter;
      try {
        getter = target.getDeclaredMethod("get" + StringUtils.capitalize(field.getName()));
      } catch (NoSuchMethodException var9) {
        continue;
      }

      getters.put(field.getName(), Tuple.of(field, getter));
    }

    return getters;
  }

  public static Map<String, Tuple<Field, Method>> getFieldSetters(Class<?> target) {
    Field[] fields = target.getDeclaredFields();
    Map<String, Tuple<Field, Method>> setters = new HashMap(fields.length, 1.0F);
    Field[] var3 = fields;
    int var4 = fields.length;

    for(int var5 = 0; var5 < var4; ++var5) {
      Field field = var3[var5];

      Method setter;
      try {
        setter = target.getDeclaredMethod("set" + StringUtils.capitalize(field.getName()), field.getType());
      } catch (NoSuchMethodException var9) {
        continue;
      }

      setters.put(field.getName(), Tuple.of(field, setter));
    }

    return setters;
  }

  private ReflectUtils() {
  }

}
